﻿package com.sxhalo.manage.web.action;

import java.util.*;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.sxhalo.manage.map.SysApplications;
import com.sxhalo.manage.service.IBaseService;
import com.sxhalo.manage.utils.Pager;
import com.sxhalo.manage.web.form.ApplicationsForm;

public class ApplicationsAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private int draw = 1;

	private int start = 0;

	private int length = 20;

	private ApplicationsForm applications;

	// 将会被Struts2序列化为JSON字符串的对象
	private Map<String, Object> dataMap;

	private List<ApplicationsForm> dataList;

	// 业务层
	private IBaseService<SysApplications> myService;

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public ApplicationsForm getApplications() {
		return applications;
	}

	public void setApplications(ApplicationsForm applications) {
		this.applications = applications;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public List<ApplicationsForm> getDataList() {
		return dataList;
	}

	public void setDataList(List<ApplicationsForm> dataList) {
		this.dataList = dataList;
	}

	public IBaseService<SysApplications> getMyService() {
		return myService;
	}

	public void setMyService(IBaseService<SysApplications> myService) {
		this.myService = myService;
	}

	/**
	 * 构造方法
	 */
	public ApplicationsAction() {
		// 初始化Map对象
		dataMap = new HashMap<String, Object>();
	}

	public String getEntity() {
		try {
			SysApplications entity = myService
					.findById(applications.getAppId());
			BeanUtils.copyProperties(entity, applications);
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String getGridData() {
		// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
		dataMap.clear();
		// 获取数据
		try {
			int page = start == 0 ? 1 : start / length + 1;
			int size = length < 0 ? Integer.MAX_VALUE : length;
			Pager<SysApplications> list = myService.findPagerList(page, size,
					"", "");
			dataMap.put("draw", draw);
			dataMap.put("recordsTotal", list.getTotal());
			dataMap.put("recordsFiltered", list.getTotal());
			dataMap.put("data", list.getList());
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String getAllData() {
		// 获取数据
		try {
			List<ApplicationsForm> data = new ArrayList<ApplicationsForm>();
			List<SysApplications> list = myService.findAll();
			for (SysApplications po : list) {
				ApplicationsForm vo = new ApplicationsForm();
				BeanUtils.copyProperties(po, vo);
				data.add(vo);
			}
			dataList = data;
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String addEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysApplications entity = new SysApplications();
		BeanUtils.copyProperties(applications, entity);
		try {
			myService.save(entity);
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public String editEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysApplications entity = myService.findById(applications.getAppId());
		entity.setAppNo(applications.getAppNo());
		entity.setAppName(applications.getAppName());
		entity.setAppPath(applications.getAppPath());
		entity.setAppDescription(applications.getAppDescription());
		entity.setAppIsdisable(applications.getAppIsdisable());
		try {
			myService.update(entity);
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public String delEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		try {
			String ids = ServletActionContext.getRequest().getParameter("ids");
			String[] arr = ids.split(",");
			for (String id : arr) {
				SysApplications entity = myService.findById(Integer.parseInt(id));
				myService.delete(entity);
			}
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}
}
